-- NewBagPanel

local ListViewVertical = require("app.widget.listview.ListViewVertical")
local NewBagRender = import(".renders.NewBagRender")

local Top_OpItemNum = 5 -- 上方可操作item个数

local ClassRef = app.mvc.createPanelClass(...)



function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.NewBagPanelNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._curSelectedIndex = 1 -- 默认选中全部

	if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_PACK_CLEARUP) then
		self._ui["Button_refresh"]:setVisible( true )
	else
		self._ui["Button_refresh"]:setVisible( false )
	end
	self._ui["Button_refresh"]:addClickEventListener(function()
			executeFunc(self._onPanelHandler, "BagPanel_sortItem")
			self._ui["Button_refresh"]:setTouchEnabled(enable)
			display.setBtnTouchEnable(self._ui["Button_refresh"], false)
			self._ui["Button_refresh"]:performWithDelay(function()
				display.setBtnTouchEnable(self._ui["Button_refresh"], true)
			end, 2)
		end)

	if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_PACK_LEVELUP) then
		self._ui["Button_up"]:setVisible( true )
	else
		self._ui["Button_up"]:setVisible( false )
	end
	self._ui["Button_up"]:addClickEventListener(function()
			executeFunc(self._onPanelHandler, "BagPanel_showBagUp")
		end)

	if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_PACK_LOTSPLITTING) then
		self._ui["Button_decompose"]:setVisible( true )
	else
		self._ui["Button_decompose"]:setVisible( false )
	end
	self._ui["Button_decompose"]:addClickEventListener(function()
			executeFunc(self._onPanelHandler, "BagPanel_decompose")
		end)

	for i = 1, Top_OpItemNum do
		self._ui["Panel_top"]:getChildByName( "Button_"..i ):addClickEventListener(function( sender )
			local btnName = sender:getName()
			local selectedIndex = string.sub( btnName, string.len(btnName), string.len(btnName) )
			if tonumber( selectedIndex ) ~= self._curSelectedIndex then
				self._curSelectedIndex = tonumber( selectedIndex )
				self:refreshSelectedTag()
			end
		end)
	end

	self._bag_name = self._ui["bag_name"] -- 背包名字格子数

	-- init scrollView
	local params = {
		renderClass = NewBagRender,
		isMultipleLayer = true,
		perNum = 5,
		paddingLeft = 5,
		paddingRight = 5,
		gapY = 4,
		allEventListen = function(sender, eventType)
			if eventType == ccui.ScrollviewEventType.scrolling then -- 滚动中
				executeFunc(self._onPanelHandler, "BagPanel_scrolling")
			elseif eventType == ccui.ScrollviewEventType.bounceStart then
				executeFunc(self._onPanelHandler, "BagPanel_bounceStart")
			elseif eventType == ccui.ScrollviewEventType.bounceStop then
				executeFunc(self._onPanelHandler, "BagPanel_bounceStop")
			end
		end,
		cellEventProxy = function(eventName, itemData)
			executeFunc(self._onPanelHandler, "BagPanel_clickItem", itemData)
		end,
	}
	self._listview = ListViewVertical.new( self._ui["ScrollView"], params )
	self:refreshSelectedTag()
end

function ClassRef:refreshSelectedTag()
	for i = 1, Top_OpItemNum do
		self._ui["Panel_top"]:getChildByName( "Button_"..i ):setBright( true )
		self._ui["Panel_top"]:getChildByName( "name_"..i ):setOpacity( 255 * 0.45 ) -- 45%透明度
	end
	self._ui["Panel_top"]:getChildByName( "Button_"..self._curSelectedIndex ):setBright( false )
	self._ui["Panel_top"]:getChildByName( "name_"..self._curSelectedIndex ):setOpacity( 255 )

	-- todo
	if self._onPanelHandler then
		executeFunc(self._onPanelHandler, "BagPanel_changeTag", self._curSelectedIndex)
	end
end

function ClassRef:registerOnOperate( cb )
	self._onPanelHandler = cb
end

-- 刷新背包名字及可升级状态
function ClassRef:updateBagInfo(bagInfo, curNum, maxNum)
	local bagInfoName = bagInfo.name .. ":" .. curNum .. "/" .. maxNum
	self._bag_name:setString( bagInfoName )

	-- local isFull = curNum >= maxNum
	-- self._bag_name:setColor(isFull and display.COLOR_RED or cc.c3b( 98, 98, 98 ))

	if bagInfo.nextConf then
		self._ui["Button_up"]:setVisible( true )
	else
		self._ui["Button_up"]:setVisible( false )
	end
end
-- 刷新分解按钮去色状态
function ClassRef:refreshBreakBtnTouchEnabled( b )
	display.setBtnTouchEnable( self._ui["Button_decompose"], b )
end
-- 刷新出售按钮状态
function ClassRef:refreshSellBtn(b)
end

function ClassRef:getScrollView()
	return self._listview
end

function ClassRef:remove()
	
end

function ClassRef:reloadListView(data, minDataLen, keepPos)
	local startIndex = 1
	if keepPos then
		startIndex = nil
	end
	self._listview:reloadData(data, {minDataLen = minDataLen, startIndex = startIndex})
end

function ClassRef:setMask()

end

function ClassRef:setSelectedStateByItem(item, trueOrFalse)
	-- local items = self._listview:getData()
	if not item then
		return
	end
	self._listview:foreachCellModels(function(model, renderData)
		if renderData and item.uid == renderData.uid then
			if model.cell then
				model.cell:setSelected(trueOrFalse)
			else
				model:setUIState("selected", trueOrFalse)
			end
			return true
		end
	end)
end

function ClassRef:setAllCellsSelectedState(trueOrFalse)
	self._listview:foreachCellModels(function(model, renderData)
		if renderData then
			if model.cell then
				model.cell:setSelected(trueOrFalse)
			else
				model:setUIState("selected", trueOrFalse)
			end
		end
	end)
end

function ClassRef:setAllCellsMaskState(trueOrFalse)
	self._listview:foreachCellModels(function(model, renderData)
        if renderData then
            if renderData.tid == Constant.Secondary_Crystal then    --次级强化水晶特殊处理
                if not UD:getCrystalCanBreak() then
                    if model.cell then
                        model.cell:setMask(trueOrFalse)
                    else
                        model:setUIState("mask", trueOrFalse)
                    end
                end
            else
                if not renderData:isCanBreak() then
                    if model.cell then
                        model.cell:setMask(trueOrFalse)
                    else
                        model:setUIState("mask", trueOrFalse)
                    end
                end
            end
        end
	end)
end

function ClassRef:resetCellsState()
	self._listview:foreachCellModels(function(model, renderData)
		if renderData then
			if model.cell then
				model.cell:setMask(false)
				model.cell:setSelected(false)
			else
				model:setUIState("mask", false)
				model:setUIState("selected", false)
			end
		end
	end)
end



return ClassRef
